package com.rzjm.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.rzjm.api.dao.ChargerMapper;
import com.rzjm.api.dao.ChargerPowerRoleMapper;
import com.rzjm.api.entity.Charger;
import com.rzjm.api.entity.ChargerPowerRole;
import com.rzjm.api.service.ChargerPowerRoleService;
import com.rzjm.api.service.ChargerService;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 *  设备充电记录Service业务层处理
 *
 * @author ruoyi
 * @date 2022-05-07
 */
@Service
public class ChargerPowerRoleServiceImpl extends BaseServiceImpl<ChargerPowerRoleMapper, ChargerPowerRole> implements ChargerPowerRoleService {

    @Resource
    private ChargerPowerRoleMapper powerRoleMapper;
    @Resource
    private ChargerPowerRoleService powerRoleService;
    @Resource
    private ChargerMapper chargerMapper;
    @Resource
    private ChargerService chargerService;
    @Resource
    protected RedisTemplate<String, Object> redisTemplate;

    @Override
    public ChargerPowerRole getSinglePriceByChargerPower(int chargerId, double power) {
        ChargerPowerRole powerRole = null;
        Charger charger = chargerService.getChargerInfoById(chargerId);
        if(charger != null){
            int isEstate = charger.getIsEstate()!= null ? charger.getIsEstate() : 0;
            int estateId = charger.getEstateId()!= null ? charger.getEstateId() : 0;
            int chargerRoleId = charger.getChargerRoleId() != null ? charger.getChargerRoleId() : 0;
            if(isEstate == 1){
                if(chargerRoleId > 0){
                    powerRole = getRoleByEstate(isEstate,estateId, charger.getChargerRoleType());
                }else{
                    powerRole = getRoleByEstate(isEstate,estateId, charger.getChargerRoleType());
                }
            } else{
                List<ChargerPowerRole> roles = getRolesByChargerId(charger);
                if(CollUtil.isNotEmpty(roles)){
                    for (ChargerPowerRole role : roles){
                        if(role.getRoleType() == 0){
                            if(role.getMinPower() < power && power <= role.getMaxPower()){
                                powerRole = role;
                                break;
                            }
                        }else{
                            powerRole = role;
                            break;
                        }
                    }
                }
            }
        }
        return powerRole;
    }


    @Override
    public ChargerPowerRole getSinglePriceByChargerPower(Charger charger, double power) {
        ChargerPowerRole powerRole = null;
        if(charger != null){
            int isEstate = charger.getIsEstate()!= null ? charger.getIsEstate() : 0;
            int estateId = charger.getEstateId()!= null ? charger.getEstateId() : 0;
            int chargerRoleId = charger.getChargerRoleId()!= null ? charger.getChargerRoleId() : 0;
            if(isEstate == 1){
                if(chargerRoleId > 0){
                    powerRole = getRoleByEstate(isEstate,estateId, charger.getChargerRoleType());
                }else{
                    powerRole = getRoleByEstate(isEstate,estateId, charger.getChargerRoleType());
                }
            } else{
                List<ChargerPowerRole> roles = getRolesByChargerId(charger);
                if(CollUtil.isNotEmpty(roles)){
                    for (ChargerPowerRole role : roles){
                        if(role.getRoleType() == 0){
                            if(role.getMinPower() < power && power <= role.getMaxPower()){
                                powerRole = role;
                                break;
                            }
                        }else{
                            powerRole = role;
                            break;
                        }
                    }
                }
            }
        }
        return powerRole;
    }


    @Override
    public void resetRoleData(String prov,String city, int roleType) {
        QueryWrapper<ChargerPowerRole> qw = new QueryWrapper<>();
        qw.eq("prov", prov);
        qw.eq("city", city);
        qw.eq("role_type", roleType);
        qw.eq("state", 1);
        List<ChargerPowerRole> roles = new ArrayList<>();
        String key = "charger_power_role_" + prov + "_" + city + "_" + roleType;
        roles = powerRoleMapper.selectList(qw);
        redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(roles));
    }

    private List<ChargerPowerRole> getRolesByChargerId(Charger charger){
        QueryWrapper<ChargerPowerRole> qw = new QueryWrapper<>();
        qw.eq("prov",charger.getProv());
        qw.eq("city",charger.getCity());
        qw.eq("state", 1);
        List<ChargerPowerRole> roles = new ArrayList<>();
        String key = "charger_power_role_" + charger.getProv() + "_" + charger.getCity() + "_" + charger.getChargerRoleType();
        boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
        if (blHasKey) {
            Object object = redisTemplate.opsForHash().get(key, key);
            String itemDicts = object != null ? object.toString() : "";
            if (StrUtil.isNotEmpty(itemDicts)) {
                JSONArray tableData = JSONArray.parseArray(itemDicts);
                roles = JSONArray.parseArray(JSONUtil.toJsonStr(tableData), ChargerPowerRole.class);
            } else {
                roles = powerRoleMapper.selectList(qw);
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(roles));
            }
        } else {
            roles = powerRoleMapper.selectList(qw);
            redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(roles));
        }
        return roles;
    }

    private ChargerPowerRole getRoleByEstate(int isEstate,int estateId,int roleType){
        ChargerPowerRole chargerPowerRole = null;
        QueryWrapper<ChargerPowerRole> qw = new QueryWrapper<>();
        qw.eq("is_estate", isEstate);
        qw.eq("estate_id", estateId);
        qw.eq("role_type", roleType);
        qw.last("limit 1");
        String key = "charger_power_role_estate_" + estateId + "_" + roleType;
        boolean blHasKey = redisTemplate.opsForHash().hasKey(key,key);
        if (blHasKey) {
            Object object = redisTemplate.opsForHash().get(key, key);
            String itemDicts = object != null ? object.toString() : "";
            if (StrUtil.isNotEmpty(itemDicts)) {
                JSONObject jsonObject = JSONUtil.parseObj(itemDicts);
                chargerPowerRole = JSONUtil.toBean(JSONUtil.toJsonStr(jsonObject), ChargerPowerRole.class);
            } else {
                chargerPowerRole = powerRoleMapper.selectOne(qw);
                redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(chargerPowerRole));
            }
        } else {
            chargerPowerRole = powerRoleMapper.selectOne(qw);
            redisTemplate.opsForHash().put(key, key, JSONUtil.toJsonStr(chargerPowerRole));
        }
        return chargerPowerRole;
    }



}
